﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Admin_AdminLogin : System.Web.UI.Page
{
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand cmd;
    private DataSet ds;
    private string connectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        connect = new SqlConnection(connectionString);
        connect.Open();
        ds = new DataSet();
        if (!IsPostBack)
        {
            if (Request.Cookies["UName"] != null)
                txtUser.Text = Request.Cookies["UName"].Value;
            if (Request.Cookies["PWD"] != null)
                txtPass.Attributes["value"] = Request.Cookies["PWD"].Value;
            if (Request.Cookies["UName"] != null && Request.Cookies["PWD"] != null)
                cbRemeber.Checked = true;
        }
        RememberMe();
    }
    protected void RememberMe()
    {
        if (cbRemeber.Checked)
        {
            Response.Cookies["UName"].Value = txtUser.Text;
            Response.Cookies["PWD"].Value = txtPass.Text;
            Response.Cookies["UName"].Expires = DateTime.Now.AddMinutes(5);
            Response.Cookies["PWD"].Expires = DateTime.Now.AddMinutes(5);
        }
        else
        {
            Response.Cookies["UName"].Expires = DateTime.Now.AddMinutes(-1);
            Response.Cookies["PWD"].Expires = DateTime.Now.AddMinutes(-1);
        }
    }
    protected bool checkLogin(string name, string pass)
    {
        bool check = false;
        try
        {
            if (connect.State == ConnectionState.Open)
            {
                connect.Close();
            }
            connect.Open();
            cmd = new SqlCommand("spAdminLog", connect);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@user", SqlDbType.VarChar).Value = name;
            cmd.ExecuteNonQuery();
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 0)
            {
                //lblStatus.Text = "User Name and password is not correct";
                check = false;
            }
            else if (ds.Tables[0].Rows[0][1].ToString() != pass)
            {
                //lblStatus.Text = "Password is not correct";
                //lblStatus.Text = ds.Tables[0].Rows[0][1].ToString() ;
                check = false;
            }
            else
            {
                //lblStatus.Text = "Welcome " + ds.Tables[0].Rows[0][0].ToString();
                //Session["admin"] = "Admin";
                Session["Admin"] = "admin";
                check = true;
            }
        }
        catch (Exception ex)
        {
            connect.Close();
        }
        return check;
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        //RememberMe();
        string user = txtUser.Text;
        string pass = txtPass.Text;
        //checkLogin(user, pass);
        if (checkLogin(user, pass))
        {
            Response.Redirect("AdminProducts.aspx");
        }
        else
        {
            lblStatus.Text = "User Name and password is not correct";
        }
        //
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtPass.Text = "";
        txtUser.Text = "";
        txtPass.Attributes["value"] = "";
    }
}